<template>
  <el-dialog title="销售预测计划产品选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">

      <el-row>
        <el-col :span="8">
          <el-form-item label="产品编码" prop="itemCode">
            <el-input
              v-model="queryParams.itemCode"
              placeholder="请输入产品编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="产品名称" prop="itemName">
            <el-input
              v-model="queryParams.itemName"
              placeholder="请输入产品名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="需求日期" prop="demandDate">
            <el-date-picker
              clearable
              v-model="queryParams.demandDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择需求日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8">
          <el-form-item label="计划编码" prop="planCode">
            <el-input
              v-model="queryParams.planCode"
              placeholder="请输入销售计划编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    <el-table v-loading="loading" :data="planLineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="产品编号" align="center" prop="itemCode" width="200px" />
      <el-table-column label="产品名称" width="150px" align="center" prop="itemName" :show-overflow-tooltip="true" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="销售计划编号" align="center" prop="planCode" />
      <el-table-column label="需求日期" align="center" prop="demandDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.demandDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { approvedList } from "@/api/mes/xs/planLine";
export default {
  name: "MultiPlanLineSelect",
  props: {
    itemOrProduct: null,
  },
  data() {
    return {
      // 弹窗开关
      showFlag: false,
      // 选中行IDS
      selectedPlanLineIds: [],
      // 选中行
      selectedRows: [],
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 计划产品表格数据
      planLineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemCode: null,
        itemName: null,
        planCode: null,
        demandDate: null,
        itemOrProduct: this.itemOrProduct,
      },
    };
  },
  created() {
    this.selectedPlanLineIds = [];
    this.selectedRows = [];
    this.getList();
  },
  watch: {
    showFlag(newVal) {
      if (newVal) {
        this.selectedPlanLineIds = [];
        this.selectedRows = [];
      }
    }
  },
  methods: {
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      this.queryParams.itemOrProduct = this.itemOrProduct
      approvedList(this.queryParams).then(response => {
        this.planLineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.selectedRows= val
    },
    confirmSelect() {
      if (this.selectedRows.length === 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }

      this.$emit('onSelected', this.selectedRows);
      this.showFlag = false;
      this.selectedRows = [];
    }
  }
};
</script>
